package net.oov.identify.model;

import net.oov.identify.Buck;
import net.oov.identify.SequenceFreqContainer;

public class TTestModel extends CheckModelAdapter{
	
	private Double minNG;
	private SequenceFreqContainer fc;
	private Double valueBuffered;
	
	public TTestModel(SequenceFreqContainer fc, Double minNG) {
		super();
		this.fc = fc;
		this.minNG = minNG;
	}
	
	/**
	 * t测试 统计
	 * Txz(y) = P(z|y)-p(y|x)/sqrt(方差(P(z|y))+方差(P(y|x)))
	 * P(z|y) = P(zy)/P(y) = r(zy)/r(y)
	 * 方差(P(z|y)) 可计算为 r(zy)/r(y)^2
	 * 
	 * Txz(y) = (r(zy)/r(y)-r(yx)/r(x))/sqrt(r(zy)/r(y)^2+r(yx)/r(x)^2)
	 */
	public Double value(StatusNode ct, CharSequence cs) {
		Buck buck = new Buck(fc.getDicForward());
		Double p = null;
		for(int i=0;i<cs.length();i++){
			if(i==0){
				p=1d;//((double)wcr.freq(cs.subSequence(0, 1)))/numWord;
			}else{
				p*=((double)buck.freq(cs.subSequence(0, i+1)))/buck.freq(cs.subSequence(i, i+1));
			}
		}
		return p;
	}

	public Boolean check(StatusNode ct, CharSequence cs ) {
		valueBuffered = value(ct,cs);
		Boolean ret = minNG <= valueBuffered;
		if(!ret)wl(cs+",T:"+valueBuffered);
		return ret;
	}

	public static void wl(Object s){
		System.out.println(s);
	}
	
	public Double value(){
		return valueBuffered;
	}

	public String info(StatusNode ct, CharSequence cs) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
